package com.kummuna;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.facebook.Session;
import com.kummuna.ui.Detectivemap;
import com.kummuna.ui.MainFragment;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;

public class LogOn extends FragmentActivity implements
		MainFragment.FBAuthenListener {
	private MainFragment mainFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		try {
			PackageInfo info = getPackageManager().getPackageInfo(
					"com.kummuna", PackageManager.GET_SIGNATURES);
			for (Signature signature : info.signatures) {
				MessageDigest md = MessageDigest.getInstance("SHA");
				md.update(signature.toByteArray());
				Log.d("KeyHash:",
						Base64.encodeToString(md.digest(), Base64.DEFAULT));
			}
		} catch (NameNotFoundException e) {

		} catch (NoSuchAlgorithmException e) {

		}

		if (savedInstanceState == null) {
			// Add the fragment on initial activity setup
			mainFragment = new MainFragment();
			mainFragment.addThrowListener(this);
			getSupportFragmentManager().beginTransaction()
					.add(android.R.id.content, mainFragment).commit();
		} else {
			// Or set the fragment from restored state info
			mainFragment = (MainFragment) getSupportFragmentManager()
					.findFragmentById(android.R.id.content);
		}
		// setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.log_on, menu);
		return true;
	}

	@Override
	public void onSuccess() {
		// TODO Auto-generated method stub
		
		Intent Gmap = new Intent(getApplicationContext(), Detectivemap.class);
		Gmap.putExtra("uid", "");
		Gmap.putExtra("uname", "");
		Gmap.putExtra("img", "");
		Gmap.putExtra("flag", "1");
		startActivityForResult(Gmap, 1);
		// startActivity(Gmap);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		if(requestCode == 1){
			if(intent !=null && intent.hasExtra("LogOut") && intent.getExtras().getBoolean("LogOut")){
				;
			}else{
				finish();
			}
		}
	}

}
